<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/testTpl1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!-- InstanceBeginEditable name="doctitle" -->
    <title>TestPage</title>
    <!-- InstanceEndEditable -->
    <!-- InstanceBeginEditable name="head" -->
    <!-- InstanceEndEditable -->
    <link rel="stylesheet" type="text/css" href="../style/google/KitchenSink.css">
    <!-- InstanceBeginEditable name="additionalStyle" -->
    <!-- InstanceEndEditable -->

    <script type="text/javascript">
    var djConfig = {
        isDebug: true
        ,debugAtAllCosts: true
    };
    </script>

    <script type="text/javascript" src="../../../trimpath/template-1.0.38.js"></script>
    <script type="text/javascript" src="../../../dojo/deploy/dojo.js"></script>
    <script type="text/javascript" src="../../../dojo-lotex/dojo-lotex.js"></script>
    <script type="text/javascript" src="../../velcro.js"></script>

    <!-- InstanceBeginEditable name="dojoRequire" -->

    <script type="text/javascript">
    dojo.require("lotex.util.Tree");
    dojo.require("lotex.io");
    dojo.require("lotex.string");

    dojo.hostenv.writeIncludes();
    </script>

    <!-- InstanceEndEditable -->
    <!-- InstanceBeginEditable name="headScript" -->

    <script type="text/javascript">
/*
dojo.declare("TreeFilePaser", null,
    function(filecontent) {
        this._content = filecontent;
    },
{
}
*/

getSelectItemTypeId = function(name) {
}

function createSelectItemType(objname) {
    var content = "objname="+objname+"&dsporder=1";
    var kw = {
        url: lotexConfig.baseurl +
        "/ServiceAction/com.velcro.base.selectitem.servlet.SelectitemtypeAction?action=create",
        postContent: content,
        method: "post",
        mimetype: "text/plain",
        load: function(type, data, evt) {
            d("添加选择项类型: " + objname + " 成功。");
        },
        error: function(t, e) {
            alert("Error!");
            d(t, e);
        }
    };
    dojo.io.bind(kw);
}

TreeFilePaser = function(content) {
    // TODO \r\n \n??
    var ca = content.split("\r\n");

    var ladder = [-1];
    var sc;
    var ft = new lotex.util.TreeNode("_ROOT_");

    var lastNode = ft;

    for(var i=0;i<ca.length;i++) {
        sc = prespace(ca[i]);

        var selItem = {
            space: sc,
            content: ca[i]
        };
        var si = new lotex.util.TreeNode(ca[i]);
        //var si = new lotex.util.TreeNode(selItem);

        var lastSpace = ladder[ladder.length-1];

        if(sc>lastSpace) {
            lastNode.addChild(si);
            ladder.push(sc);
        } else if(sc==lastSpace) lastNode.addBrother(si);
        else if(sc<lastSpace) {
            var siRoot = lastNode;
            while (sc < ladder[ladder.length-1]) {
                ladder.pop();
                siRoot = siRoot.getParent();
                var dc = ladder[ladder.length-1];
                d(dc);
            }
            siRoot.addBrother(si);
        }

        lastNode = si;
    }
    d("TreeFilePaser OK!");

    return ft;
}

var tree;

    var kw = {
        url: lotexConfig.resurl + "/selectitem/opu.selectitem",
        sync: true,
        mimetype: "text/plain",
        load: function(type, data, evt) {
            tree = TreeFilePaser(data);
            dojo.debug(tree);
            tree.forEach(function(node){
                var item = dojo.string.trim(node.getValue());
                if(lotex.string.isBlank(item)) return;
                d("添加新选择项:" + item);

                node.forEach(function(i){
                    var item = dojo.string.trim(i.getValue());
                    if(lotex.string.isBlank(item)) return;
                    d("添加选择项项目:" + item);

                    if(i.haveChildren()) d("描述："+i.getChildren()[0].getValue());
                });

            });
        },
        error: function(t, e) {
            alert("Error!");
            dd(t, e);
        }
    };
    dojo.io.bind(kw);

    </script>

    <!-- InstanceEndEditable -->
</head>
<body>
    <table style="width: 100%" cellspacing="0" cellpadding="0">
        <tbody>
            <tr>
                <td style="vertical-align: top" align="left">
                    <div class="ks-Info">
                        Testpage</div>
                </td>
            </tr>
            <tr>
                <td style="vertical-align: top" align="left">
                    <table class="ks-Sink" cellspacing="0" cellpadding="0">
                        <tbody>
                            <tr>
                                <td style="vertical-align: top; width: 100%; height: 100%" align="left">
                                    <div id="container">
                                        <!-- InstanceBeginEditable name="htmlContainer" -->
                                        <!-- InstanceEndEditable -->
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
        </tbody>
    </table>
    <fieldset>
        <legend>Output</legend>
        <div id="debug.output">
        </div>
    </fieldset>
</body>
<!-- InstanceEnd --></html>
